package com.suitsoft.ut.service.thesis;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.dao.account.UserDao;
import com.suitsoft.ut.dao.thesis.ThesisDao;
import com.suitsoft.ut.entity.account.Authority;
import com.suitsoft.ut.entity.account.Role;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;

/**
 * 题 管理 服务层类
 *@author Suineg
 */
@Service
@Transactional
public class ThesisManager {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ThesisDao dao;
	@Autowired
	private UserDao userDao;
	
	public List<Thesis> getAll(){
		return dao.getAll();
	}
	
	public void save(Thesis thesis){
		if(thesis.getId() == null || thesis.getId() == 0l){
			ThesisFile thesisFile = new ThesisFile();
			thesisFile.setThesisFlag(Constants.THESIS_FLAG_LITI);
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_MANAGER);
			
			thesis.setThesisFile(thesisFile);
			thesisFile.setThesis(thesis);
			
			dao.save(thesis);
		}else{
			dao.save(thesis);
		}
	}
	
	public void delete(Long id){
		dao.delete(id);
	}
	
	public Thesis get(Long id){
		return dao.get(id);
	}
	
	public List<Thesis> get(List<Long> ids){
		return dao.get(ids);
	}
	
	public Page<Thesis> findBy(Page<Thesis> page,List<PropertyFilter> filters){
		return dao.findPage(page, filters);
	}

	public Page<Map<String,Object>> find(Page<Map<String,Object>> page ,String teacher,String group,Long stauts,Long flag){
		
		return dao.findThesis(page, teacher, group, stauts,flag);
	}
	
	/**
	 * 保存评审
	 * @param thesis
	 * @param thesisFile
	 * @param flag
	 * @throws Exception
	 */
	public void savePingShen(Thesis thesis,ThesisFile thesisFile,Long flag) throws Exception{
		String content = null;
		Date date = null;
		String theacherName = null;
		
		thesis = dao.get(thesis.getId());
		if(flag == 1){
			content = thesisFile.getSetThePingyueContent();
			date = thesisFile.getSetThePingyueDate();
			theacherName = thesisFile.getSetThePingyueTeacher();
			
			thesisFile  = thesis.getThesisFile();
			
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_FIRST_AUDIT);
			thesisFile.setSetThePingyueContent(content);
			thesisFile.setSetThePingyueDate(date);
			thesisFile.setSetThePingyueTeacher(theacherName);
		}else if(flag == 2){
			content = thesisFile.getSetTheGroupReview();
			date = thesisFile.getSetTheGroupDesignTime();
			theacherName = thesisFile.getSetTheGroupDesign();
			
			
			thesisFile  = thesis.getThesisFile();
			
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_SECOND_AUDIT);
			thesisFile.setSetTheGroupDesign(theacherName);
			thesisFile.setSetTheGroupReview(content);
			thesisFile.setSetTheGroupDesignTime(date);
		}else if(flag ==3){
			thesisFile  = thesis.getThesisFile();
			
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_THIRD_AUDIT);
		}else if(flag == 4){
			content = thesisFile.getSetTheBaseReview();
			date = thesisFile.getSetTheBaseDesignTime();
			theacherName = thesisFile.getSetTheBaseDesign();
			
			thesisFile  = thesis.getThesisFile();
			
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_FIRTHE_AUDIT);
			
			thesisFile.setSetTheBaseDesign(theacherName);
			thesisFile.setSetTheBaseDesignTime(date);
			thesisFile.setSetTheBaseReview(content);
		}
		
		dao.save(thesis);
	}
	
	public Page<Thesis> findBypropertys(Page<Thesis> page,List<PropertyFilter> filters){
		return dao.findPage(page, filters);
	}
	
	public List<Thesis> findNoStudent(){
		
		return dao.findByNoStudent();
	}
	
	public Page<Thesis> findByConfirmStudent(Page<Thesis> page,String teacherName,String studentName,String titile){
		LoginUser loginUser = UserHolder.getLoginUser();
		if(loginUser == null){
			return null;
		}
		User user = null;
		try {
			user = userDao.get(loginUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		if(user == null){
			return null;
		}
		
		String collegeCode = null;
		for(Role r : user.getRoleList()){
			String authNames = r.getAuthNames();
			if(authNames.indexOf(Constants.CN_SCHOOL_ADMIN) != -1){		//校管理员 可查询所有
				collegeCode = null;
				break;
			}
			for(Authority a : r.getAuthorityList()){
				if(Constants.CN_COLLEGE_ADMIN.equals(a.getName())){
					collegeCode = user.getCollegeCode();
					break;
				}
			}
		}
		
		Teacher teacher = null;
		teacher = user.getTeacher();
		if(teacher == null){
			return null;
		}
		
		return dao.findByConfirmStudent(page,teacherName,studentName,titile,collegeCode);
	}
	
	public List<Thesis> findByTeacherNo(String teacherNo){
		return dao.findBy("teacherNo", teacherNo);
	}
}

